package edu.cuit.avatar.controller;

import com.alibaba.fastjson2.JSONObject;
import edu.cuit.avatar.entity.Book;
import edu.cuit.avatar.factory.InstanceFactory;
import edu.cuit.avatar.service.BookService;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author <a href="mailto:1020zhaodan@163.com">Adan</a>
 * @version 1.0
 * @date 2025/8/1 17:05
 */
@WebServlet("/book/find")
public class BookFindServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private BookService service = InstanceFactory.create("book-service");
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        var id = Integer.valueOf(req.getParameter("id"));
        var book = service.findById(id);
        resp.setContentType("application/json; charset=utf-8");
        PrintWriter out = resp.getWriter();
        out.println(JSONObject.toJSONString(book));
    }
}
